<?php

namespace app\controller;

use app\model\ArticleClass;
use app\model\Label;
use think\Controller;
use think\Request;

class Article extends Controller
{

    //登录验证
    public function _initialize()
    {
        if ( !session( 'isLogin' ) ) {
            $this->redirect( 'login/index' );
        }
    }

    /**
     * 显示文章列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $list = db( 'article' )->where(['status'=>1])->paginate( 15 );


        $this->assign( 'list', $list );

        // 渲染模板输出
        return view();

    }

    /**
     * 显示文章添加修改页
     * @param bool|false $id
     * @return \think\response\View
     */
    public function show( $id = false )
    {

        //获取标签
        $labelList = db( 'label' )->select();

        //获取分类
        $classModel = new articleClass();

        $classList = $classModel->getClass( [ false, 1 ] );

        $this->assign( 'classList', $classList );

        $this->assign( 'labelList', $labelList );

        return view();
    }

    /**
     * 显示分类添加修改页
     * @param bool|false $id
     * @return \think\response\View
     */
    public function classshow( $id = false )
    {
        $classModel = new articleClass();

        $list = $classModel->getClass( [ false, 0 ] );

        $this->assign( 'classList', $list );

        return view();
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save( Request $request )
    {

        //获取Post传值
        $articleInfo = $request->post();


        $articleModel = new \app\model\Article();

        $re = $articleModel->saveArticle( $articleInfo );

        if ( $re[ 'status' ] ) {

            $this->success( '添加成功' );

        } else {

            $this->error( $re[ 'info' ] );

        }

    }

    /**
     * 分类管理页面
     * @param int $limit
     * @return mixed
     */
    public function classlist( $limit = 15 )
    {

        $classModel = new articleClass();

        $list = $classModel->getClass( [ true, $limit ] );

        $this->assign( 'list', $list );

        // 渲染模板输出
        return $this->fetch();

    }

    /**
     * 添加分类
     * @param Request $request
     * @param $className
     */
    public function classsave( Request $request, $className )
    {
        if ( $className ) {

            $fid = $request->post( 'fid' );

            $classModel = new articleClass();

            $re = $classModel->saveClass( $className, $fid );

            if ( $re ) {

                $this->success( '添加成功', url( 'article/classList' ) );

            } else {

                $this->error( '失败了,要不再试一次' );

            }

        } else {

            $this->error( '填个分类名会死啊' );

        }
    }

    /**
     * 标签添加修改页面
     * @return \think\response\View
     */
    public function labelshow()
    {


        $list = db( 'label' )->select();

        $this->assign( 'list', $list );

        return view();

    }

    /**
     * 新增标签
     * @param Request $request
     * @param $name
     * @return \think\response\Json
     */
    public function saveLabel( Request $request, $name )
    {
        $labelModel = new label();

        $re = $labelModel->saveLabel( $name );

        return json( $re );
    }

    /**
     * 删除标签
     * @param Request $request
     * @param $id
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function deleteLabel( Request $request, $id )
    {
        if ( $id ) {

            $status = db( 'label' )->delete( $id );

            if ( $status ) {

                $return = [ 'status' => true ];

            } else {

                $return = [ 'status' => false, 'info' => '失败了,要不重新来一次?' ];

            }

        } else {

            $return = [ 'status' => false, [ 'info' => '不传参数怎么删?' ] ];

        }

        return json( $return );

    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read( $id )
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit( $id )
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update( Request $request, $id )
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete( $id )
    {

        if ( $id ) {

            $re = db( 'article' )->where( [ 'id' => $id ] )->update( [ 'status' => 0 ] );

            if ( $re ) {

                $this->success( '操作成功' );

            } else {

                $this->error( '数据更新错误，要不再试试？' );

            }

        } else {

            $this->error( '参数错误' );

        }

    }
}
